package com.yitao.cms.controller.common;


import com.google.common.collect.Maps;
import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.service.user.cmsUser.OperationUserService;
import com.yitao.cms.utils.UpdateFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping(value = "/cms", produces = {"application/json"})
@Api(description = "运营后台公共", tags = {"cms-common"})
public class CommonComtroller {

    private static final Logger logger = LoggerFactory.getLogger(CommonComtroller.class);

    @Autowired
    private UpdateFileUtils updateFileUtils;
    @Value("${shop_product_url}")
    private String shopUrl;

    @Value("${fastdfs.head}")
    private String headUrl;

    @Autowired
    private OperationUserService userService;

    @ApiOperation("获取图片地址请求前缀")
    @RequestMapping(value = "/upload/head", method = RequestMethod.POST)
    public DataOutput getHeadUrl() {
        HashMap<String, String> urlHeadUrl = Maps.newHashMap();
        urlHeadUrl.put("url", headUrl);
        return new DataOutput(urlHeadUrl);
    }

    @ApiOperation("图片上传接口")
    @RequestMapping(value = "/upload/picture", method = RequestMethod.POST)
    public DataOutput uploadPicture(@ApiParam(value = "图片文件", required = true) @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        try {
            return updateFileUtils.updateFile(file);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("文件上传失败");
            return new DataOutput(CommonStatus.SERVER_ERROR.getCode());
        }
    }

    @ApiOperation("获取登录用户门店链接")
    @RequestMapping(value = "/findShopIdUrl", method = RequestMethod.POST)
    public DataOutput findShopIdUrl(HttpServletRequest httpServletRequest) {
        OperationUserCmsModel operationUserCmsModel = UserSession.getUserSession(httpServletRequest);
        if (operationUserCmsModel != null && operationUserCmsModel.getShopId() != null) {
            OperationUserCmsModel userInfoById = userService.findUserInfoById(operationUserCmsModel.getId());
            return new DataOutput(shopUrl + userInfoById.getShopId());
        }
        return new DataOutput();
    }

}
